#![allow(non_snake_case)]
#![allow(unused)]

use c2rust003::prompt;
// #[allow(unused)]
use c2rust003::{chatgml::C2RustGLM, BigModel};
// #[allow(unused)]
use c2rust004::{generate_rust_project, translate_project_to_single_dir, translate_prompt_project, translate_prompt_sign_dir_mti_project, translate_c_project_with_c2rust};
// #[allow(unused)]
use cargo_at::translate_prompt;
use clap::Parser;
use dotenv::dotenv;

use std::fs::File;
#[allow(unused_imports)]
use std::io::{self, Write};
#[allow(unused_imports)]
use std::path::Path;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(short, long)]
    in_dir: String, // 输入目录

    #[arg(short, long)]
    main_file: String, // 主文件名
    // Number of times to greet
    // #[arg(short, long, default_value_t = 1)]
    // out_file: u8,
    #[arg(short, long)]
    out_dir: Option<String>, // 输出目录
}

#[tokio::main]
async fn main() {
    dotenv().ok();
    #[allow(unused)]
    let mut c_2_rust_glm = C2RustGLM::new().await;

    let args = Args::parse();

    let base_dir = args.in_dir.as_str();
    let main_file = args.main_file; // 这里需要处理 不传参则默认为转译整个目录里面的所有内容

    // let all_names = [
    //     "arraylist",
    //     "avl-tree",
    //     "binary-heap",
    //     "binomial-heap",
    //     "bloom-filter",
    //     "compare-int",
    //     "compare-pointer",
    //     "compare-string",
    //     "hash-int",
    //     "hash-pointer",
    //     "hash-string",
    //     "hash-table",
    //     "list",
    //     "queue",
    //     "rb-tree",
    //     "set",
    //     "slist",
    //     "sortedarray",
    //     "trie",
    //     "add",
    //     "test-cpp",
    // ]; // 这里需要处理 不传参则默认为转译整个目录里面的所有内容

    // if !all_names.contains(&name.as_str()) {
    //     println!("file not found");
    //     return;
    // }

    // let names = vec![name.clone()];

    // let prompts = translate_prompt(base_dir, names.clone());
    // println!("prompts: {:?}", prompts.len());

    // let project_prompts = translate_prompt_sign_dir_mti_project(base_dir, names);
    // println!("project_prompt_output: {:?}", project_prompts);

//     for project_prompt in project_prompts {
//         println!("开始生成项目: {}", project_prompt.filename);
//         let prompt = r#"
// Transpile the following C Code to Rust Code
// Generate a project and all functions should be public by pub key word and 
// When import other module please use ```use {{project_name}}::{{module_name}}::*;``` format but not ```use crate::*;``` if do not defined the module in the current project should use ```mod {{module_name}};``` first,
// Also need to generate the meta data for the project, such as the Cargo.toml and so on;
// If has use not std crate should add the dependencies into the Cargo.toml, ex: libc and so on;
// If has test code, please generate the test code in the test folder and add the [[tests]] metadata in the Cargo.toml file;
// Rust project should not contain the C file ex: xx.h xx.c;
// Project should generate the main function and the main file with name main.rs;
// Please generate the code in Markdown format, and the code should be in rust code block format;

// Please use the following code prompt:
// "#.to_string() + &project_prompt.content;
//         translate_prompt_project(name.clone(), &mut c_2_rust_glm, prompt, BigModel::ChatGml).await;
//         println!("生成项目: {} 完成", project_prompt.filename);
//     }


    // for prompt in prompts {
    //     // TODO:
    //     generate_rust_project(name.clone(), &mut c_2_rust_glm, prompt, BigModel::ChatGml).await;
    // }

    let project_prompts = translate_project_to_single_dir(base_dir, &main_file);

    match project_prompts {
        Ok(project_prompt) => {
            // println!("project_prompt: {:?}", project_prompt);
            // 打印json格式
            // println!("project_prompt: {:?}", serde_json::to_string(&project_prompt).unwrap());
            // 写入文件
            let mut file = File::create("project_prompt.json").unwrap();
            file.write_all(serde_json::to_string(&project_prompt).unwrap().as_bytes()).unwrap();
            translate_c_project_with_c2rust(base_dir, &main_file, project_prompt).await;
        }
        Err(e) => {
            println!("转译失败 Error: {}", e);
        }
    }




}
